﻿/*
COPYRIGHT (C) 2011-2013 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Threading.Tasks;
using System.Web.Mvc;
using ScrumPilot.Domain.BPL;
using ScrumPilot.Domain.Models;
using ScrumPilot.Helpers;

namespace ScrumPilot.Controllers
{
    [Authorize]
    public partial class HomeController : BaseController
    {
        private WallServices _wall;
        protected WallServices WallModel { get { return _wall ?? (_wall = new WallServices(TFS)); } }

        private ConfigServices _config;
        protected ConfigServices ConfigSrv { get { return _config ?? (_config = new ConfigServices()); } }

        [AllowAnonymous]
        public ActionResult About()
        {
            return View();
        }

        #region DashBoard/Index
        [HttpGet]
        public ActionResult Index()
        {
            // todo : implement a cookie with TFS informations
            SPHelpers.CheckTFSServiceInSession(HttpContext);
            if (TFS == null)
            {
                // If there is no instance of TFS in Session, log off
                return RedirectToAction("Logoff", "Account");
            }
            SPHelpers.SetSessionValues(HttpContext, this.TFS);

            if (this.ConfigSrv.GetDashBoardType() == 0)
                return View(); // Return ScrumPilot Standard Dashboard

            return View("KanbanDashBoard"); // Return Kanban dashboard
        }

        [HttpGet, NoCache]
        public JsonResult GetWallInfo(int first, int last)
        {
            return Json(WallModel.GetWallInfo(first, last, CurrentUser, CurrentCulture, this.TfsRootUrl),
                        JsonRequestBehavior.AllowGet);
        }

        [HttpGet, NoCache]
        public JsonResult GetWallInfoByDate(string lastindex)
        {
            DateTime? dt = DateTimeHelpers.ConvertFromJavaTimestamp(lastindex);
            if (dt.HasValue)
            {
                return Json(WallModel.GetWallInfo(dt.Value, CurrentUser, CurrentCulture, this.TfsRootUrl),
                            JsonRequestBehavior.AllowGet);
            }
            return null;
        }

        [HttpGet, NoCache]
        public JsonResult GetUserWallInfo(int id, int last)
        {
            return Json(WallModel.GetUserWallInfo(0, last, id, CurrentUser, CurrentCulture, this.TfsRootUrl),
                        JsonRequestBehavior.AllowGet);
        }

        [HttpGet, NoCache]
        public JsonResult AddComment(Int64 id, string comment, Boolean mail)
        {
            Boolean result = false;
            Int64 eventCommentID = WallModel.CreateComment(CurrentUser, id, comment);
            if (eventCommentID > 0)
            {
                result = true;
                // Todo : Send Mail to onwer
            }
            return Json(new Object[] { result, eventCommentID }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet, NoCache]
        public JsonResult DelComment(Int64 id)
        {
            return Json(WallModel.DeleteComment(CurrentUser, id), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public PartialViewResult WhoDoesWhat()
        {
            return PartialView();
        }

        [HttpGet]
        public PartialViewResult MainIndicator()
        {
            return PartialView();
        }

        [HttpGet]
        public PartialViewResult KeyIndicators()
        {
            return PartialView();
        }

        [HttpGet]
        public PartialViewResult IndexWall()
        {
            return PartialView();
        }

        [HttpGet]
        public PartialViewResult BugsList()
        {
            return PartialView();
        }
        #endregion

        #region DashBoard
        [HttpGet, NoCache, Authorize]
        public async Task<JsonResult> KeyPoints()
        {
            return Json(
                await ComputeProjectKeys(), JsonRequestBehavior.AllowGet
            );
        }

        // ReSharper disable CSharpWarnings::CS1998
        private async Task<KeyPoints> ComputeProjectKeys()
        // ReSharper restore CSharpWarnings::CS1998
        {
            if (TFS != null)
            {
                var kpm = new ScrumDashBoardServices(this.TFS);
                return kpm.GetKeyPoints(CurrentUser, CurrentCulture);
            }
            return null;
        }
        #endregion
    }
}